package mcom.xmks.bluetooth.mesh.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import mcom.xmks.bluetooth.mesh.R;
import mcom.xmks.bluetooth.mesh.utils.ToastUtil;

public class InputShareFileInfoDialog extends BaseDialogFragment{
   private OnShareFileResult result;
   private String filename;
   private View cancel,confirm;
   private EditText editFileName;

   public InputShareFileInfoDialog(String filename) {
      this.filename=filename.replaceAll("/","");
   }

   public static InputShareFileInfoDialog getInstance(String filename) {

      Bundle args = new Bundle();

      InputShareFileInfoDialog fragment = new InputShareFileInfoDialog(filename);
      fragment.setArguments(args);
      return fragment;
   }

   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view =inflater.inflate(R.layout.dialog_input_file_info,null);
      editFileName=view.findViewById(R.id.tvFileName);
      cancel=view.findViewById(R.id.cancel);
      confirm=view.findViewById(R.id.confirm);
      initWidget();
      return view;
   }

   @Override
   public void onDestroy() {
      super.onDestroy();
   }

   private  void initWidget(){
      editFileName.setText(filename);
       cancel.setOnClickListener(v -> dismiss());
       confirm.setOnClickListener(v -> check());
   }
   private void check(){
      String s = editFileName.getText().toString();
      if("".equals(s)){
         showToast("文件名不能为空");
         return;
      }

      if(((filename.endsWith(".hex") || filename.endsWith(".HEX")) && (s.endsWith(".hex") || s.endsWith(".HEX")))
              || ((filename.endsWith(".bin") || filename.endsWith(".BIN")) && (s.endsWith(".bin") || s.endsWith(".BIN")))){
         if(result!=null){
            result.onResult(s);
         }
         dismiss();
      }else{
         showToast("请不要修改文件类型");
      }
   }
   public void setListener(OnShareFileResult result){
      this.result=result;
   }

   public interface OnShareFileResult{
      void onResult(String filename);
   }
   private void showToast(String message){
      ToastUtil.create(requireActivity(),message);
   }
}
